package io.renren.modules.medicine.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.constant.Constant;
import io.renren.common.interceptor.DataScope;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.JsonUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.modules.medicine.dao.TaskGeneEditingDao;
import io.renren.modules.medicine.dto.TaskAdmetDTO;
import io.renren.modules.medicine.dto.TaskGeneEditingDTO;
import io.renren.modules.medicine.entity.TaskAdmetEntity;
import io.renren.modules.medicine.entity.TaskGeneEditingEntity;
import io.renren.modules.medicine.enums.TaskStatusEnum;
import io.renren.modules.medicine.service.TaskGeneEditingService;
import io.renren.modules.security.user.SecurityUser;
import io.renren.modules.security.user.UserDetail;
import io.renren.modules.sys.service.SysDeptService;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * 基因编辑
 *
 * @author sheliming shelimingming@qq.com
 * @since 1.0.0 2023-03-27
 */
@Service
public class TaskGeneEditingServiceImpl extends CrudServiceImpl<TaskGeneEditingDao, TaskGeneEditingEntity, TaskGeneEditingDTO> implements TaskGeneEditingService {
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public QueryWrapper<TaskGeneEditingEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<TaskGeneEditingEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);
        wrapper.select(TaskGeneEditingEntity.class, info -> !info.getColumn().equals("result"));

        String beginTimeString = (String) params.get(Constant.BEGIN_TIME);
        wrapper.ge(StringUtils.isNotBlank(beginTimeString), "create_date", beginTimeString);
        String endTimeString = (String) params.get(Constant.END_TIME);
        wrapper.le(StringUtils.isNotBlank(endTimeString), "create_date", endTimeString);

        if (params.get(Constant.SQL_FILTER) != null) {
            wrapper.apply(params.get(Constant.SQL_FILTER) != null, ((DataScope) params.get(Constant.SQL_FILTER)).getSqlFilter());
        }

        return wrapper;
    }

    @Override
    public void putTaskAsyn(String filePath) {
        TaskGeneEditingDTO dto = new TaskGeneEditingDTO();
        dto.setTokensFile(filePath);

        UserDetail user = SecurityUser.getUser();
        dto.setCreator(user.getId());
        dto.setCreatorName(user.getRealName());
        dto.setUpdater(user.getId());
        dto.setDeptId(user.getDeptId());
        if (user.getDeptId() != null) {
            dto.setDeptName(sysDeptService.get(user.getDeptId()).getName());
        }
        dto.setCreateDate(new Date());
        dto.setUpdateDate(new Date());
        dto.setStatus(TaskStatusEnum.CREATED.value());
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        this.save(dto);

        sendMQ(dto.getId(), dto.getTokensFile());

        dto.setStatus(TaskStatusEnum.RUNNING.value());
        this.update(dto);
    }

    private void sendMQ(Long taskId, String tokensFile) {
        MQBody mqBody = new MQBody();
        mqBody.setTaskId(taskId);
        mqBody.setTokensFile(tokensFile);

        rabbitTemplate.convertAndSend("gene_editing_exchange", "gene_editing.hh", JsonUtils.toJsonString(mqBody));
    }

    @Data
    private class MQBody {
        private Long taskId;
        private String tokensFile;
    }


}